/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gunncs;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Vector;

/**
 *
 * @author anand
 */
public class RobotModel {

    public static final int SIZE = 150;
    public static final int INSET = 300;
    //Layout = x;
    private Vector<SensorModel> sensors;

    public RobotModel() {
        sensors = new Vector<SensorModel>();
        //Preconcieved
        sensors.add(new BumpSensorModel());
        sensors.add(new RangeFinderModel());
        sensors.add(new RangeFinderModel());
        sensors.add(new ServoModel(45));
        sensors.add(new RangeFinderModel());
        sensors.add(new ServoModel(45));
        sensors.add(new BeaconSensorModel());
        sensors.add(new CompassModel());
        
    }

    //NOTE:
    //draw() uses preconcieved knowledge of Robot Layout to render Sensor data
    public void draw(Graphics g) {
    	((Graphics2D) g).setStroke(new BasicStroke(2));
        g.drawOval(INSET, INSET, SIZE, SIZE);
        g.setColor(Color.BLACK);
        if (sensors.size() > 0) {
            //Bumpsensor
            sensors.elementAt(0).drawAt(g, INSET, INSET, 0);
            //BeaconSensor
            sensors.elementAt(6).drawAt(g, INSET, INSET, 0);
            //Front RangeFinder
            sensors.elementAt(1).drawAt(g, INSET + SIZE / 2, INSET + SIZE / 2, -90);
            //Right RangeFinder
            sensors.elementAt(2).drawAt(g, INSET + SIZE / 2, INSET + SIZE / 2, -90+(-1*sensors.elementAt(3).getState()[0]));
            //Left RangeFinder
            sensors.elementAt(4).drawAt(g, INSET + SIZE / 2, INSET + SIZE / 2, -90+(sensors.elementAt(5).getState()[0]));
            //Compass
            sensors.elementAt(7).drawAt(g, INSET+ SIZE / 2, INSET+ SIZE / 2, 0);
        }
    }

    public void addSensor(SensorModel s) {
        sensors.addElement(s);
    }

    public Vector<SensorModel> getSensors() {
        return sensors;
    }

    public void updateStatus(String[] config) {
        for (int i = 0; i < config.length; i++) {
            String[] parts = config[i].split(" ");
            sensors.elementAt(i).setState(parts[1]);
        }
    }

    public static RobotModel robotFromStatus(String[] config) {
        RobotModel r = new RobotModel();
        //[Sensor id] [value];[value]...
        for (int i = 0; i < config.length; i++) {
            String[] parts = config[i].split(" ");
            switch (Integer.parseInt(parts[0].trim())) {
                case RangeFinderModel.ID:
                    RangeFinderModel rTemp = new RangeFinderModel();
                    rTemp.setState(parts[1]);
                    r.addSensor(rTemp);
                    break;
                case BumpSensorModel.ID:
                    BumpSensorModel bTemp = new BumpSensorModel();
                    bTemp.setState(parts[1]);
                    r.addSensor(bTemp);
                    break;
                case BeaconSensorModel.ID:
                    BeaconSensorModel bcTemp = new BeaconSensorModel();
                    bcTemp.setState(parts[1]);
                    r.addSensor(bcTemp);
                    break;
                case CompassModel.ID:
                    CompassModel cTemp = new CompassModel();
                    cTemp.setState(parts[1]);
                    r.addSensor(cTemp);
                    break;
                case ServoModel.ID:
                    ServoModel sTemp = new ServoModel();
                    sTemp.setState(parts[1]);
                    r.addSensor(sTemp);
                    break;
            }
        }
        return r;
    }
}
